﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using LDSP.BRL;

namespace LDSP.Util
{
    public class TaskPlanExport
    {
        public static void Export(HttpServerUtility server, DataTable source)
        {
            IDictionary<string, DataTable> sourceDic = new Dictionary<string, DataTable>();

            foreach (DataRow row in source.Rows)
            {
                if (row["favoriteID"] != null)
                {
                    string cod = row["favoriteID"].ToString();
                    if (sourceDic.ContainsKey(cod))
                    {
                        sourceDic[cod].Rows.Add(row.ItemArray);
                    }
                    else
                    {
                        DataTable dt = source.Copy();
                        dt.Rows.Clear();

                        sourceDic.Add(cod, dt);
                        dt.Rows.Add(row.ItemArray);
                    }
                }
            }

            Export(server, sourceDic);
        }

        private static void Export(HttpServerUtility server, IDictionary<string, DataTable> source)
        {
            ExcelClass excel = new ExcelClass(server.MapPath("..\\..\\..\\Templates\\TaskPlan.xls"));

            //Import all the sheets.
            foreach (string key in source.Keys)
            {
                string name = new R_EnterpriseCodification().GetEnterpriseCodificationByID(Int32.Parse(key)).Codification.Name;
                name = RemoveEnglishName(name);
                excel.ImportDataTable(excel.GenerateSheet(name), false, GetExportedDT(source[key]), 3, 1);
            }

            //Remove the 1st sheet, because it is just a template.
            if (excel.Sheets.Count > 1)
            {
                excel.RemoveSheet(0);
            }

            excel.ToExcel(@"E:\Lab\Web\ExcelImportExport\TaskPlanList.xls", DownloadType.PromptDialog);
        }

        /// <summary>
        /// 删除名称里的下划线及后面的英文
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private static string RemoveEnglishName(string name)
        {
            string result = name;
            if (name.Contains("\n"))
            {
                result = name.Substring(0, name.IndexOf("\n"));
            }

            return result;
        }

        private static DataTable GetExportedDT(DataTable source)
        {
            //修改显示文本。
            DataTable modifedDT = source.Copy();

            modifedDT.Columns.Add("CodificationName");
            modifedDT.Columns.Add("expiredDTs");
            modifedDT.Columns.Add("changeDTs");
            modifedDT.Columns.Add("statuss");

            for (int i = 0; i < source.Rows.Count; i++)
            {
                Plan ett = new Plan();
                ett.GetProperties(source.Rows[i]);
                modifedDT.Rows[i]["CodificationName"] = ett.EnterpriseCodification.Codification.Name;
                modifedDT.Rows[i]["expiredDTs"] = FormatDT(ett.ExpiredDT);
                modifedDT.Rows[i]["changeDTs"] = FormatDT(ett.ChangeDT);
                modifedDT.Rows[i]["statuss"] = ett.Status == 1 ? "是" : "否";
            }

            //导出.
            DataTable dt = new DataTable();
            dt.Columns.Add("Codification Name");
            dt.Columns.Add("Pricipal");
            dt.Columns.Add("Name");
            dt.Columns.Add("Execution");
            dt.Columns.Add("Expired Date");
            dt.Columns.Add("Process");
            dt.Columns.Add("Update Date");
            dt.Columns.Add("Comments");
            dt.Columns.Add("IsDone");

            foreach (DataRow row in modifedDT.Rows)
            {
                dt.Rows.Add(row["CodificationName"],
                    row["person"],
                    row["name"],
                    row["department"],
                    row["expiredDTs"],
                    row["progress"],
                    row["changeDTs"],
                    row["comment"],
                    row["statuss"]);
            }

            return dt;
        }

        private static string FormatDT(DateTime dt, bool isDateTime = false)
        {
            if (dt == DateTime.MaxValue)
            {
                return "";
            }
            return isDateTime ? dt.ToString("yyyy-MM-dd HH:mm:ss") : dt.ToString("yyyy-MM-dd");
        }
    }
}